package com.missmess.opengldemo.ui;

import android.opengl.GLU;
import android.os.Bundle;

import com.missmess.opengldemo.GlBaseActivity;
import com.missmess.opengldemo.model.Sphere;
import com.missmess.opengldemo.util.BufferUtils;

import java.nio.FloatBuffer;

import javax.microedition.khronos.opengles.GL10;

public class DrawSphere extends GlBaseActivity {

    private Sphere sphere;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sphere = new Sphere();
    }

    @Override
    public void drawScene(GL10 gl) {
        super.drawScene(gl);
        initScene(gl);

        sphere.draw(gl);
    }

    public static void initScene(GL10 gl){
        float[] mat_amb = {0.2f * 1.0f, 0.2f * 0.4f, 0.2f * 0.4f, 1.0f,};
        float[] mat_diff = {1.0f, 0.4f, 0.4f, 1.0f,};
        float[] mat_spec = {1.0f, 1.0f, 1.0f, 1.0f,};

        FloatBuffer mat_ambBuf = BufferUtils.float2Buffer(mat_amb);
        FloatBuffer mat_diffBuf = BufferUtils.float2Buffer(mat_diff);
        FloatBuffer mat_specBuf = BufferUtils.float2Buffer(mat_spec);

        gl.glClearColor(0.8f, 0.8f, 0.8f, 0.0f);
        gl.glEnable(GL10.GL_DEPTH_TEST);
        gl.glEnable(GL10.GL_CULL_FACE);
        gl.glShadeModel(GL10.GL_SMOOTH);

        gl.glEnable(GL10.GL_LIGHTING);
        gl.glEnable(GL10.GL_LIGHT0);

        gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_AMBIENT, mat_ambBuf);
        gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_DIFFUSE, mat_diffBuf);
        gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_SPECULAR, mat_specBuf);
        gl.glMaterialf(GL10.GL_FRONT_AND_BACK, GL10.GL_SHININESS, 64.0f);

        gl.glLoadIdentity();
        GLU.gluLookAt(gl, 0.0f, 0.0f, 10.0f,
                0.0f, 0.0f, 0.0f,
                0.0f, 1.0f, 0.0f);

    }
}
